/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.ad.feedsreader.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.interceptor.SessionAware;

import ua.ad.feedsreader.mybatis.config.MyBatisSqlSessionFactory;
import ua.ad.feedsreader.mybatis.mappers.beans.Categoria;
import ua.ad.feedsreader.mybatis.mappers.beans.Feed;
import ua.ad.feedsreader.mybatis.mappers.beans.Utilizador;
import ua.ad.feedsreader.mybatis.mappers.beans.UtilizadorOauth;
import ua.ad.feedsreader.mybatis.mappers.clients.CategoriaMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.FeedMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.UtilizadorOauthMapper;

import com.opensymphony.xwork2.ActionSupport;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.Authorization;
import twitter4j.auth.BasicAuthorization;
import twitter4j.conf.ConfigurationBuilder;

/**
 *
 * @author sexta13
 * @author ricardo
 */
public class InitFeedsReaderAction extends ActionSupport implements SessionAware {

	/**
	 *
	 */
	private final static String CONSUMER_KEY_SECRET = "UbHYn3zmeIDD5tEJYnwgaBEWPEdxAfVggGvwyj6z4Q";
	private final static String CONSUMER_KEY = "lZLuzCeR4q86Of6mo6uXQ";
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session;
	private List<Categoria> listaCategorias;
	private List<Feed> listaFeeds;
	private Integer oauthId;
	private String chaveFB;
	private String tokenTwitter;
	private String tokenSecretTwitter;

	@Override
	public String execute() throws Exception {
		Utilizador userSession = (Utilizador) getSession().get("utilizador");

		SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

		CategoriaMapper catMap = bdSession.getMapper(CategoriaMapper.class);
		FeedMapper fMapper = bdSession.getMapper(FeedMapper.class);
		UtilizadorOauthMapper userOauth = bdSession.getMapper(UtilizadorOauthMapper.class);

		List<UtilizadorOauth> lstOAuth = userOauth.selectByUserId(userSession.getId());

		for (UtilizadorOauth oAuth : lstOAuth) {
			if (oAuth != null) {
				switch (oAuth.getIdTypeExtApp()) {
				//facebook
				case 1:
					if (getSession().containsKey("chaveFb")) {
						chaveFB = (String) getSession().get("chaveFb");
						oauthId = oAuth.getId();
					}
					break;
				//twitter
				case 2:
					if (getSession().containsKey("tokenTwitter") && getSession().containsKey("tokenSecretTwitter")) {
						tokenTwitter = (String) getSession().get("tokenTwitter");
						tokenSecretTwitter = (String) getSession().get("tokenSecretTwitter");
						break;
					} else {
						String[] tempOauth = oAuth.getOauthCode().split("@@@");
						System.out.println("TTES" + tempOauth[0] + "   " + tempOauth[1]);
						tokenTwitter = tempOauth[0];
						tokenSecretTwitter = tempOauth[1];
						AccessToken at = new AccessToken(tokenTwitter, tokenSecretTwitter);

						ConfigurationBuilder confbuilder = new ConfigurationBuilder();

						confbuilder.setOAuthAccessToken(at.getToken()).setOAuthAccessTokenSecret(at.getTokenSecret()).setOAuthConsumerKey(CONSUMER_KEY).setOAuthConsumerSecret(CONSUMER_KEY_SECRET);

						Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance();
						getSession().put("TWITTER", twitter);

					}

					System.out.println("############################# " + oauthId);
				}
			}
		}
		listaCategorias = new ArrayList<Categoria>();

		setListaCategorias(catMap.selectCategoryByUserId(userSession.getId()));

		setListaFeeds(fMapper.selectFeedsByUtilizador(userSession.getId()));

		bdSession.close();
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public List<Categoria> getListaCategorias() {
		return listaCategorias;
	}

	public void setListaCategorias(List<Categoria> listaCategorias) {
		this.listaCategorias = listaCategorias;
	}

	public List<Feed> getListaFeeds() {
		return listaFeeds;
	}

	public void setListaFeeds(List<Feed> listaFeeds) {
		this.listaFeeds = listaFeeds;
	}

	/**
	 * @return the oauthId
	 */
	 public Integer getOauthId() {
		 return oauthId;
	 }

	 /**
	  * @param oauthId the oauthId to set
	  */
	 public void setOauthId(Integer oauthId) {
		 this.oauthId = oauthId;
	 }

	 /**
	  * @return the chaveFB
	  */
	 public String getChaveFB() {
		 return chaveFB;
	 }

	 /**
	  * @param chaveFB the chaveFB to set
	  */
	 public void setChaveFB(String chaveFB) {
		 this.chaveFB = chaveFB;
	 }
}
